/*
 * Copyright (c) 2024 Syntiant Corp.  All rights reserved.
 * Contact at http://www.syntiant.com
 *
 * This software is available to you under a choice of one of two licenses.
 * You may choose to be licensed under the terms of the GNU General Public
 * License (GPL) Version 2, available from the file LICENSE in the main
 * directory of this source tree, or the OpenIB.org BSD license below.  Any
 * code involving Linux software will require selection of the GNU General
 * Public License (GPL) Version 2.
 *
 * OPENIB.ORG BSD LICENSE
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 	** SDK: v112.3.5-Samsung **
*/

#include <syntiant_ilib/syntiant_portability.h>

int ERROR_PRINTF(const char * fmt, ...){
    int ret;
    va_list args;
    va_start(args, fmt);
    #ifdef __KERNEL__
    ret = vprintk(fmt, args);
    #else
    ret = vfprintf(stderr, fmt, args) + fprintf(stderr, "\n");
    #endif
    va_end(args);
    return ret;
}

#if defined(__KERNEL__)
int syntiant_get_ms_time(syntiant_ms_time *ms_time)
{
    ktime_get_ts64(ms_time);
    return 0;
}

unsigned long syntiant_get_ms_elapsed(syntiant_ms_time *ms_time)
{
    syntiant_ms_time now_time, diff_time;
    syntiant_get_ms_time(&now_time);
    diff_time = timespec64_sub(now_time, *ms_time);
    return (unsigned long)diff_time.tv_sec * 1000 +
           (unsigned long)diff_time.tv_nsec / 1000000;
}

#elif defined(__linux__)
int syntiant_get_ms_time(syntiant_ms_time *ms_time)
{
    return gettimeofday(ms_time, NULL);
}

unsigned long syntiant_get_ms_elapsed(syntiant_ms_time *ms_time)
{
    syntiant_ms_time now_time, diff_time;

    syntiant_get_ms_time(&now_time);
    timersub(&now_time, ms_time, &diff_time);

    return (unsigned long)diff_time.tv_sec * 1000 +
           (unsigned long)diff_time.tv_usec / 1000;
}

#else
/* customer platform implementation here */
int syntiant_get_ms_time(syntiant_ms_time *ms_time)
{
    struct timeval  tv;
    gettimeofday(&tv, NULL);

    *ms_time = (uint32_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000);
    return 0;
}

unsigned long syntiant_get_ms_elapsed(syntiant_ms_time *ms_time)
{
    syntiant_ms_time now_time;

    syntiant_get_ms_time(&now_time);
    return now_time - *ms_time;
}

#endif



